//http get request

String doGet(String url) {
  WiFiClient client;
  HTTPClient http;

  Serial.println("[doGet]begin >>>" + url);
  if (http.begin(client, url)) {  // HTTP
    int httpCode = http.GET();
    if ( httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)  {
      String payload = http.getString();
      return payload;
    }
    else {
      Serial.printf("[doGet] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
  else {
    Serial.printf("[doGet} Unable to connect\n");
  }
  return "";
}
//获取当前时间
void getTimeNow() {
  String payload = doGet(TIME_URL);
  Serial.println(payload);
  DynamicJsonDocument  doc(1024);
  deserializeJson(doc, payload);
  String _time = doc["data"]["t"];
  Serial.println("[TIME] get time >>" + _time);
  doc.clear();
  String _tt = _time.substring(0, (_time.length() - 3));
  Serial.println("[TIME] get set time  " + _tt.toInt());
  setTime(_tt.toInt() + 8 * 60 * 60);
}


// 获取当前天气
void getWatherNow() {
  if (WATHER_KEY == "" || WATHER_KEY == "null")
  {
    Serial.print("[WATHER]not find wather key can not get wather now..");
    return;
  }
  String url = WATHER_URL + "?key=" + WATHER_KEY + "&location=" + WATHER_LOCATION ;
  String payload = doGet(url);
  Serial.println(payload);
  DynamicJsonDocument  doc(1024);
  deserializeJson(doc, payload);
  JsonVariant  result = doc["results"];
  JsonArray resarray = result.as<JsonArray>();
  JsonVariant  nownode = resarray[0]["now"];
  String lastupdate = resarray[0]["last_update"].as<String>();
  Serial.println(lastupdate);
  //2022-04-12T10:47:27+08:00
  WATHER_UPDATE_TIME = lastupdate.substring(16, 11);
  WATHER_TEMPERATURE = nownode["temperature"].as<int>();
  WATHER_CODE =   nownode["code"].as<int>();
  Serial.println("[WATHER] WATHER_TEMPERATURE " + String( WATHER_TEMPERATURE) + " WATHER_CODE " + String( WATHER_CODE));
  doc.clear();
}




void getLife() {
  if (WATHER_KEY == "" || WATHER_KEY == "null")
  {
    Serial.print("[WATHER]not find wather key can not get wather now..");
    return;
  }
  String url = LIFE_URL + "?key=" + WATHER_KEY + "&location=" + WATHER_LOCATION ;
  String payload = doGet(url);
  Serial.println(payload);
  DynamicJsonDocument  doc(1024);
  deserializeJson(doc, payload);

  JsonVariant  result = doc["results"];
  JsonArray resarray = result.as<JsonArray>();
  JsonVariant  suggestionnode = resarray[0]["suggestion"];

  LIFE_CAR_WASHINNG = suggestionnode["car_washing"]["brief"].as<String>(); ;
  LIFE_DRESSING = suggestionnode["dressing"]["brief"].as<String>(); ;
  LIFE_FLU =  suggestionnode["flu"]["brief"].as<String>(); ;
  LIFE_SPORT =  suggestionnode["sport"]["brief"].as<String>(); ;
  LIFE_TRAVEL = suggestionnode["travel"]["brief"].as<String>(); ;
  LIFE_UV = suggestionnode["uv"]["brief"].as<String>(); ;

  Serial.print(" LIFE_CAR_WASHINNG " + LIFE_CAR_WASHINNG );
  Serial.print(" LIFE_DRESSING " + LIFE_DRESSING );
  Serial.print(" LIFE_FLU " + LIFE_FLU );
  Serial.print(" LIFE_SPORT " + LIFE_SPORT );
  Serial.print(" LIFE_TRAVEL " + LIFE_TRAVEL );
  Serial.print(" LIFE_UV " + LIFE_UV );
  Serial.println(" ");
  doc.clear();
}

void getDAily() {
  String url = DAILY_URL + "?key=" + WATHER_KEY + "&location=" + WATHER_LOCATION + "&days=3";

  String payload = doGet(url);
  Serial.println(payload);
  DynamicJsonDocument  doc(2048);
  deserializeJson(doc, payload);
  JsonVariant  result = doc["results"];
  JsonArray resarray = result.as<JsonArray>();
  JsonArray dailys =  resarray[0]["daily"].as<JsonArray>();
  Serial.println("  daily length >>>" + dailys.size() );

  Serial.println("1 >>>" + dailys[0].as<String>() );
  Serial.println("2 >>>" + dailys[1].as<String>() );
  Serial.println("3 >>>" + dailys[2].as<String>() );

  TODAY_DAY_CODE = dailys[0]["code_day"].as<int>();
  TODAY_NIGHT_CODE = dailys[0]["code_night"].as<int>(); // 夜晚天气
  TODAY_HIGH_TEMPERATURE = dailys[0]["high"].as<String>(); // 温度高
  TODAY_LOW_TEMPERATURE = dailys[0]["low"].as<String>(); // 温度低
  TODAY_WIND_DIRECTION = dailys[0]["wind_direction"].as<String>(); // 风向
  TODAY_WIND_DIRECTION_DEGREE = dailys[0]["wind_direction_degree"].as<String>(); // 风向角度
  TODAY_WIND_SPEED = dailys[0]["wind_speed"].as<String>(); // 风速
  TODAY_WIND_SCALE = dailys[0]["wind_scale"].as<String>(); // 风力
  TODAY_HUMIDITY = dailys[0]["humidity"].as<String>(); // 湿度;


  TOMORROW_DATE = dailys[1]["date"].as<String>();
  TOMORROW_DATE=TOMORROW_DATE.substring(6);
  TOMORROW_DAY_CODE = dailys[1]["code_day"].as<int>();
  TOMORROW_NIGHT_CODE = dailys[1]["code_night"].as<int>(); // 夜晚天气
  TOMORROW_HIGH_TEMPERATURE = dailys[1]["high"].as<String>(); // 温度高
  TOMORROW_LOW_TEMPERATURE = dailys[1]["low"].as<String>(); // 温度低
  TOMORROW_WIND_DIRECTION = dailys[1]["wind_direction"].as<String>(); // 风向
  TOMORROW_WIND_DIRECTION_DEGREE = dailys[1]["wind_direction_degree"].as<String>(); // 风向角度
  TOMORROW_WIND_SPEED = dailys[1]["wind_speed"].as<String>(); // 风速
  TOMORROW_WIND_SCALE = dailys[1]["wind_scale"].as<String>(); // 风力
  TOMORROW_HUMIDITY = dailys[1]["humidity"].as<String>(); // 湿度;

  AFTER_TOMORROW_DATE = dailys[2]["date"].as<String>();
  AFTER_TOMORROW_DATE=AFTER_TOMORROW_DATE.substring(6);
  AFTER_TOMORROW_DAY_CODE = dailys[2]["code_day"].as<int>();
  AFTER_TOMORROW_NIGHT_CODE = dailys[2]["code_night"].as<int>(); // 夜晚天气
  AFTER_TOMORROW_HIGH_TEMPERATURE = dailys[2]["high"].as<String>(); // 温度高
  AFTER_TOMORROW_LOW_TEMPERATURE = dailys[2]["low"].as<String>(); // 温度低
  AFTER_TOMORROW_WIND_DIRECTION = dailys[2]["wind_direction"].as<String>(); // 风向
  AFTER_TOMORROW_WIND_DIRECTION_DEGREE = dailys[2]["wind_direction_degree"].as<String>(); // 风向角度
  AFTER_TOMORROW_WIND_SPEED = dailys[2]["wind_speed"].as<String>(); // 风速
  AFTER_TOMORROW_WIND_SCALE = dailys[2]["wind_scale"].as<String>(); // 风力
  AFTER_TOMORROW_HUMIDITY = dailys[2]["humidity"].as<String>(); // 湿度;

  Serial.print(" TODAY_DAY_CODE " + TODAY_DAY_CODE );
  Serial.print(" TODAY_NIGHT_CODE " + TODAY_NIGHT_CODE );
  Serial.print(" TODAY_HIGH_TEMPERATURE " + TODAY_HIGH_TEMPERATURE );
  Serial.print(" TODAY_LOW_TEMPERATURE " + TODAY_LOW_TEMPERATURE );
  Serial.print(" TODAY_WIND_DIRECTION " + TODAY_WIND_DIRECTION_DEGREE );
  Serial.print(" TODAY_WIND_SPEED " + TODAY_WIND_SPEED );
  Serial.print(" TODAY_WIND_SCALE " + TODAY_WIND_SCALE );
  Serial.print(" TODAY_HUMIDITY " + TODAY_HUMIDITY );
  Serial.println(" ");


  Serial.print(" TOMORROW_DATE " + TOMORROW_DATE );
  Serial.print(" TOMORROW_DAY_CODE " + TOMORROW_DAY_CODE );
  Serial.print(" TOMORROW_NIGHT_CODE " + TOMORROW_NIGHT_CODE );
  Serial.print(" TOMORROW_HIGH_TEMPERATURE " + TOMORROW_HIGH_TEMPERATURE );
  Serial.print(" TOMORROW_LOW_TEMPERATURE " + TOMORROW_LOW_TEMPERATURE );
  Serial.print(" TOMORROW_WIND_DIRECTION " + TOMORROW_WIND_DIRECTION_DEGREE );
  Serial.print(" TOMORROW_WIND_SPEED " + TOMORROW_WIND_SPEED );
  Serial.print(" TOMORROW_WIND_SCALE " + TOMORROW_WIND_SCALE );
  Serial.print(" TOMORROW_HUMIDITY " + TOMORROW_HUMIDITY );
  Serial.println(" ");

  Serial.print(" AFTER_TOMORROW_DATE " + AFTER_TOMORROW_DATE );
  Serial.print(" AFTER_TOMORROW_DAY_CODE " + AFTER_TOMORROW_DAY_CODE );
  Serial.print(" AFTER_TOMORROW_NIGHT_CODE " + AFTER_TOMORROW_NIGHT_CODE );
  Serial.print(" AFTER_TOMORROW_HIGH_TEMPERATURE " + AFTER_TOMORROW_HIGH_TEMPERATURE );
  Serial.print(" AFTER_TOMORROW_LOW_TEMPERATURE " + AFTER_TOMORROW_LOW_TEMPERATURE );
  Serial.print(" AFTER_TOMORROW_WIND_DIRECTION " + AFTER_TOMORROW_WIND_DIRECTION_DEGREE );
  Serial.print(" AFTER_TOMORROW_WIND_SPEED " + AFTER_TOMORROW_WIND_SPEED );
  Serial.print(" AFTER_TOMORROW_WIND_SCALE " + AFTER_TOMORROW_WIND_SCALE );
  Serial.print(" AFTER_TOMORROW_HUMIDITY " + AFTER_TOMORROW_HUMIDITY );
  Serial.println(" ");
  doc.clear();
}
